//
//  TMShopHomeController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMShopHomeController.h"
#import "TMCourseCollectCell.h"
#import "TMShopHomeHeadView.h"
#import "TMClassDetailController.h"
#import "TMWorkingTeacherController.h"
#import "TMTeacherDetailController.h"
#import "TMCourseSaleController.h"
#import "TMHomeViewModel.h"
#import "TMShopCourseModel.h"
#import "TMCommodityDetailController.h"
@interface TMShopHomeController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *shopHomeTab;
@property (weak, nonatomic) IBOutlet UIView *barView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *barSegment;
@property (nonatomic, strong)TMShopHomeHeadView *headView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) TMHomeViewModel *viewModel;
@property (nonatomic, strong) TMShopProveModel *shopModel;
@end

@implementation TMShopHomeController
- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
-(TMShopHomeHeadView *)headView{
    if (!_headView) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"TMShopHomeHeadView" owner:self options:nil].lastObject;
    }
    return _headView;
}
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    if (self.barSegment.selectedSegmentIndex) {
//
//    }
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"机构详情";
    self.shopHomeTab.delegate = self;
    self.shopHomeTab.dataSource = self;
    self.shopHomeTab.backgroundColor = [UIColor whiteColor];
    self.shopHomeTab.tableFooterView = [UIView new];
    [self.shopHomeTab registerNib:[UINib nibWithNibName:@"TMCourseCollectCell" bundle:nil] forCellReuseIdentifier:@"TMCourseCollectCell"];
    self.barSegment.tintColor = [UIColor hx_colorWithHexRGBAString:@"#05B48D"];
    self.barSegment.momentary = YES;
    self.viewModel = [[TMHomeViewModel alloc]init];
    self.shopHomeTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.viewModel getShopHomeDataWithShop:self.shopid Success:^(id responseObject, id responseObject1) {
            self.dataArray = responseObject;
            self.shopModel = self.dataArray[2];
            [self.shopHomeTab reloadData];
            // [self noResultViewShowORNot];
            [self.shopHomeTab.mj_header endRefreshing];
        } AndFiled:^(NSString *error) {
            //[self noResultViewShowORNot];
            [self.shopHomeTab.mj_header endRefreshing];
        }];
    }];
    [self.shopHomeTab.mj_header beginRefreshing];
}
- (IBAction)barSegmentedAction:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        //在职教师
        TMWorkingTeacherController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMWorkingTeacherController"];
        vc.cid = self.shopid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (sender.selectedSegmentIndex == 1) {
        //课程出售
        TMCourseSaleController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCourseSaleController"];
        vc.cid = self.shopid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (sender.selectedSegmentIndex == 2) {
        //商家评论
        TMTeacherDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTeacherDetailController"];
        vc.isTeacherDetail = NO;
        vc.tid = self.shopid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (sender.selectedSegmentIndex == 3) {
        //客服
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
        //聊天界面的聊天类型
        conversationVC.conversationType = ConversationType_PRIVATE;
        conversationVC.targetId = [NSString stringWithFormat:@"%ld", (long)self.shopModel.uid];
        conversationVC.title = @"客服";
        RCUserInfo *info = [[RCUserInfo alloc]initWithUserId:[NSString stringWithFormat:@"%ld",[TMUserManager deaufatManager].userModel.uid] name:[TMUserManager deaufatManager].userModel.userName portrait:[NSString stringWithBaseUrl:[TMUserManager deaufatManager].userModel.userimage]];
        [RCIM sharedRCIM].currentUserInfo = info;
        [self.navigationController pushViewController:conversationVC animated:YES];
    }
}

#pragma delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count>0) {
        return 2;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TMCourseCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMCourseCollectCell"];
    if (indexPath.row == 0) {
        if ([self.dataArray[0] count] == 0) {
            cell.headImageV.hidden = YES;
        }else{
            cell.headImageV.image = [UIImage imageNamed:@"精品课堂"];
        }
        cell.listArray = self.dataArray[0];
    }else{
        if ([self.dataArray[0] count] == 0) {
            cell.headImageV.hidden = YES;
        }else{
            cell.headImageV.image = [UIImage imageNamed:@"杰出老师"];
        }
        cell.listArray = self.dataArray[3];
    }
    cell.block = ^(NSInteger index) {
        if (indexPath.row == 0) {
            TMShopCourseModel *models = self.dataArray[0][index];
            
            TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
            
            vc.rid = models.hcid;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            TMShopTeacherModel *modelt = self.dataArray[3][index];
        TMTeacherDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTeacherDetailController"];
        vc.isTeacherDetail = YES;
            vc.tid = modelt.htid;
        [self.navigationController pushViewController:vc animated:YES];
        }
        
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //return ((116 +22)*3 +61) *kWindowW/375;
    if (indexPath.row == 0) {
        NSArray *arr = self.dataArray[0];
        if (arr.count == 0) {
            return 0;
        }else{
            if (arr.count%3== 0) {
                return ((116+22)*arr.count/3 +61)*kWindowW/375;
            }else{
                return ((116+22)*(arr.count/3 +1) +61)*kWindowW/375;
            }
        }
    }else{
        NSArray *arr = self.dataArray[3];
        if (arr.count == 0) {
            return 0;
        }else{
            if (arr.count%3== 0) {
                return ((116+22)*arr.count/3 +61)*kWindowW/375;
            }else{
                return ((116+22)*(arr.count/3 +1) +61)*kWindowW/375;
            }
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    [self headView];
    __weak typeof(self) weakself = self;
    if (self.dataArray.count >0) {
        _headView.headArray = self.dataArray;
          TMShopProveModel *model = self.dataArray[2];
        _headView.block = ^(BOOL isselect) {
            [weakself.viewModel getShopCollectionWith:model.uid Success:^(id responseObject) {
                                            if ([responseObject isEqualToString:@"收藏成功"]) {
                                                weakself.headView.collectBtn.selected = YES;
                                            }else{
                                                weakself.headView.collectBtn.selected = NO;
                    }
            } failed:^(NSString *error) {
                weakself.headView.collectBtn.selected = NO;
            }];
        };
    }
    return _headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (kWindowW == 320) {
        return 332;
    }else{
        return 332 ;
    }
}
//            [weakself.viewModel   :model.pid :^(id responseObject) {
//                            [TMProgressHUD showSuccessWithStyleDarkStatus:responseObject];
//                            if ([responseObject isEqualToString:@"收藏成功"]) {
//                                weakself.headView.collectBtn.selected = YES;
//                            }else{
//                                weakself.headView.collectBtn.selected = NO;
//                            }
//                        } AndFiled:^(NSString *error) {
//                        weakself.headView.collectBtn.selected = NO;
//                        }];
@end
